﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime;

namespace ToddMcDermid.SSIS
{
    #region CLASS: PauseTaskEditor
    public partial class frmPauseTaskEditor : Form
    {
        #region Private Variables
        private TaskHost _taskHost;
        private Connections _connections;
        #endregion

        #region Constructor
        public frmPauseTaskEditor(TaskHost taskHost, Connections connections)
        {
            this._taskHost = taskHost;
            this._connections = connections;
            InitializeComponent();
        }
        #endregion

        #region Form Event Handlers
        private void PauseTaskEditor_Load(object sender, EventArgs e)
        {
            object propertyObjectValue;
                
            #region Set up PauseType
            this.cboPauseType.Items.Clear();
            foreach (string pauseTypeString in Enum.GetNames(typeof(PauseTask.PauseTypes)))
            {
                this.cboPauseType.Items.Add(pauseTypeString);
            }
            string pauseTypeValue = "";
            propertyObjectValue = (PauseTask.PauseTypes)this._taskHost.Properties["PauseType"].GetValue(this._taskHost);
            if (propertyObjectValue != null)
            {
                pauseTypeValue = propertyObjectValue.ToString();
            }
            this.cboPauseType.SelectedItem = pauseTypeValue;
            if (this.cboPauseType.SelectedIndex < 0)
            {
                this.cboPauseType.SelectedIndex = 0;
            }
            #endregion

            #region Set up PauseFor
            int pauseForValue = 0;
            propertyObjectValue = this._taskHost.Properties["PauseFor"].GetValue(this._taskHost);
            if (propertyObjectValue != null)
            {
                pauseForValue = (int)propertyObjectValue;
            }
            this.nudPauseFor.Value = pauseForValue;
            #endregion

            #region Set up PauseUntil
            DateTime pauseUntilValue = DateTime.Now;
            propertyObjectValue = this._taskHost.Properties["PauseUntil"].GetValue(this._taskHost);
            if (propertyObjectValue != null)
            {
                try
                {
                    pauseUntilValue = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + propertyObjectValue.ToString());
                }
                catch { }
            }
            this.dtpPauseUntil.Value = pauseUntilValue;
            #endregion

            this.EnableControls();
        }
        #endregion

        #region Control Event Handlers
        private void cboPauseType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.EnableControls();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this._taskHost.Properties["PauseType"].SetValue(this._taskHost, Enum.Parse(typeof(PauseTask.PauseTypes), (string)this.cboPauseType.SelectedItem));
            this._taskHost.Properties["PauseFor"].SetValue(this._taskHost, (int)this.nudPauseFor.Value);
            this._taskHost.Properties["PauseUntil"].SetValue(this._taskHost, this.dtpPauseUntil.Value.ToString("hh:mm:ss.fff"));

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void EnableControls()
        {
            bool showPauseFor = ((PauseTask.PauseTypes)Enum.Parse(typeof(PauseTask.PauseTypes), (string)this.cboPauseType.SelectedItem) == PauseTask.PauseTypes.ElapsedTime);
            if (showPauseFor)
            {
                this.lblPauseForUntil.Text = "Pause For:";
            }
            else
            {
                this.lblPauseForUntil.Text = "Pause Until:";
            }
            this.nudPauseFor.Visible = showPauseFor;
            this.dtpPauseUntil.Visible = !showPauseFor;
        }
    }
    #endregion
}
